﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEngine.DrawEvaluator
{
    /// <summary>
    /// Spanish Draughts Evalutor Class
    /// </summary>
    public class SpanishDrawEvaluator: DrawEvaluator
    {
        /// <summary>
        /// Maximum number of moves for Spanish checkers
        /// </summary>
        private const int keySpanishMaximumNumberOfMoves = 20;

        /// <summary>
        /// Overrided maximum number of moves
        /// </summary>
        protected override int MaximumNumberOfMoves
        {
            get
            {
                return keySpanishMaximumNumberOfMoves;
            }
        }

        /// <summary>
        /// Checks presence of king on the main diagonal - inverted the main diagonal
        /// due to the playing on white fields
        /// </summary>
        private bool IsKingOnMainDiagonal(enumFigureColor color)
        {
            for (int i = 0; i < GameBoard.Size; i++)
            {
                enumFigureType figureType = GameBoard.GetField(i, 7 - i);
                if (figureType != enumFigureType.Nothing)
                {
                    if (figureType.IsKing() == true)
                    {
                        if (figureType.FigureColor() == color)
                            return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Checks special positions for Spanish checkers
        /// </summary>
        /// <returns></returns>
        protected override int? IsSpecialPosition()
        {
            FigureCounts cnt = GetCurrentFigureCounts();

            int menCount = 0;
            int kingCount = 0;
            enumFigureColor oppositeFigureColor = enumFigureColor.White;

            // check if there is one king on one player side
            if (cnt.WKCnt == 1 && cnt.WMCnt == 0)
            {
                menCount = cnt.BMCnt;
                kingCount = cnt.BKCnt;
                oppositeFigureColor = enumFigureColor.White;
            }
            else if (cnt.BKCnt == 1 && cnt.BMCnt == 0)
            {
                menCount = cnt.WMCnt;
                kingCount = cnt.WKCnt;
                oppositeFigureColor = enumFigureColor.Black;
            }
            else return null;

            if (kingCount == 3 && menCount == 0 && IsKingOnMainDiagonal(oppositeFigureColor) == false)
            {
                return 13;
            }

            return null;
        }
    }
}
